package com.example.mediaplayer.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


/** 
 * @author 作者ylh  E-mail:504367857@qq.com
 * @version 创建时间：2015-1-18 下午3:33:59 
 * 类说明 
 */
public class FileService {
	private DbOpenHelper dbOpenHelper;
	private SQLiteDatabase db;

	public FileService(Context context) {
		this.dbOpenHelper = new DbOpenHelper(context);
		
	}

	/**
	 * 保存file
	 * 
	 */
	public long save(int id,String filePath) {
		db = dbOpenHelper.getWritableDatabase();
	
		ContentValues values = new ContentValues();
		values.put("id", id);
		values.put("filePath", filePath);
		long a = db.insert("sessionTable", null, values);
		db.close();
		return a;
	}


	public String getMusic() {
		db = dbOpenHelper.getWritableDatabase();
		String session="";
		Cursor cursor = db.rawQuery(
				"select filePath from fileTable where id=?",
				new String[] { "1" });
		if(cursor.moveToFirst()){
			
			session=cursor.getString(0);
		}
		cursor.close();
		db.close();
		return session;
	}
	
	/**
	 * 判断数据库中是否已有file 只在登录时使用
	 * 
	 * @param id
	 * @return
	 */
	public Boolean hasFile(int id) {
		db = dbOpenHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from fileTable where id=?",
				new String[] { id+"" });

		if (cursor.moveToFirst()) {// 判断游标是否为空
			cursor.close();
			db.close();
			return true;
		} else {
			cursor.close();
			db.close();
			return false;
		}

	}

}
